/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-03-15 11:45:28
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-03-16 14:03:13
 */
'use static'
const kaoJwt = require('koa-jwt')
const jwt = require('jsonwebtoken')
const config = require('../utils/config')
module.exports={
    /**
     * 获取用户登录
     * @params{string} data加密信息
     * @return{string} 返回生成的token
     */
    signToken:(data)=>{
        try {
            return jwt.sign({
                data
            }, config.security.secretKey,{expiresIn:config.security.expiresIn})
        } catch (error) {
            console.log(error)
        }
         next()
    },
    verifyToken:async(ctx,next)=>{
        console.log(1111)
        try {
            console.log(ctx.request.headers.authorization)
            console.log(config.security.secretKey)
            ctx.jwtData = await jwt.verify(ctx.request.headers.authorization, config.security.secretKey)
            await next()
        } catch (error) {
            console.log(error)
             throw {
                 code: 401,
                 message: error.message
             }
        }
    }
}